package com.erbao.gc;

/**
 * -XX:-DoEscapeAnalysis -XX:-EliminateAllocations -XX:-UseTLAB
 * @author CaoBaoYin
 * @since 2022/4/3 11:37
 */
public class TestLAB {

    class User {
        int id;
        String name;

        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }

    User u;
    void alloc(int i) {
        // 因为创建的对象，alloc方法外无法使用，就叫做无逃逸
        // new User(i, "name"+i);
        // 有全部变量接收就说明有逃逸
        u = new User(i, "name"+i);

    }

    public static void main(String[] args) {
        TestLAB test = new TestLAB();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000_0000; i++) {
            test.alloc(i);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
